import {
  Controller,
  Delete,
  Get,
  // HttpException,
  // HttpStatus,
  /*Logger, */ Post,
  UnauthorizedException,
} from '@nestjs/common';
import { Logger } from 'nestjs-pino';
import { UserService } from './user.service';
import { ConfigService } from '@nestjs/config';
import { ConfigEnum } from 'src/enum/config.enum';
import { User } from './user.entity';

@Controller('user')
export class UserController {
  // nestjs 自带的日志
  // private logger = new Logger(UserController.name);
  constructor(
    private userService: UserService,
    private configService: ConfigService,
    private logger: Logger,
  ) {
    this.logger.log('UserController init');
    this.userService = userService;
  }

  @Get()
  getUsers(): any {
    // this.logger.log('请求 getUsers');
    // return {
    //   code: 0,
    //   data: [],
    //   msg: 'success',
    // };
    const user = { isAdmin: false };
    if (!user.isAdmin) {
      // throw new HttpException(
      //   'User is not admin, Forbidden to access getAllUsers',
      //   HttpStatus.FORBIDDEN,
      // );

      throw new UnauthorizedException('用户没有权限');
    }
    const db = this.configService.get(ConfigEnum.DB);
    console.log(db);
    const DB_URL = this.configService.get('DB_URL');
    console.log(DB_URL);
    return this.userService.getUsers();
  }

  @Post()
  addUser(): any {
    return this.userService.addUser();
  }

  @Get('/find/all')
  findAll(): any {
    return this.userService.findAll();
  }

  updateUser(): any {
    // to do 传递参数id
    // todo 异常处理
    const user = {
      username: 'newname',
    } as User;
    return this.userService.update(1, user);
  }

  @Delete()
  deleteUser(): any {
    return this.userService.remove(1);
  }

  @Get('/profile')
  getUserProfile(): any {
    return this.userService.findProfile(2);
  }
}
